/*
 * Hai Phan
 * 
 * TCSS 342 - Winter 2010
 * Assignment 6B - Spreadsheet
 * March 4, 2010
 */

package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * EditDialog. Keeps track of the Row/Column
 * Edit Dialog.
 * 
 * @author Hai Phan
 */
public class EditDialog {
	
	/**
	 * The main spreadsheet frame.
	 */
	private JFrame my_main_frame;
	
	/**
	 * The edit dialog.
	 */
	private JDialog my_edit_dialog;
	
	/**
	 * The spreadsheet table.
	 */
	private SpreadSheetTable my_table;
	
	/**
	 * The user edit row textfield.
	 */
	private JTextField my_row_textfield;
	
	/**
	 * The user edit column textfield.
	 */
	private JTextField my_column_textfield;
	
	/**
	 * The textfield formula displayed to the user.
	 */
	private JTextField my_formula_textfield;
	
	/**
	 * The main gui.
	 */
	private SpreadSheetGUI my_gui;
	
	/**
	 * EditDialog constructor, initializes the EditDialog
	 * with the parameter values.
	 * 
	 * @param the_table the spreadsheet table.
	 * @param the_value_textfield the textfield value displayed to the user.
	 * @param the_main_frame the main spreadsheet frame.
	 */
	public EditDialog(final SpreadSheetTable the_table, 
			final JTextField the_value_textfield, 
			final JFrame the_main_frame,
			final SpreadSheetGUI gui) {
		my_gui = gui;
		my_table = the_table;
		my_main_frame = the_main_frame;
		my_formula_textfield = the_value_textfield;
		my_row_textfield = new JTextField();
		my_column_textfield = new JTextField();
		my_edit_dialog = new JDialog(my_main_frame, "Edit Rows & Columns", true);
		setUpEditDialog();
	}
	
	/**
	 * Sets up the edit dialog box.
	 */
	private void setUpEditDialog() {
		my_edit_dialog.add(createJPanel()); 	//adds a JPanel to the JDialog
		my_edit_dialog.setResizable(false);		//disables resizing
		my_edit_dialog.pack();
		my_edit_dialog.setLocationRelativeTo(null);
		my_edit_dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		my_edit_dialog.setVisible(true);
	}
	
	/**
	 * Create and returns a JPanel that ask for the user
	 * for the new rows and columns.
	 * 
	 * @return panel a panel asking for the rows/columns.
	 */
	private JPanel createJPanel() {
		JPanel panel = new JPanel(new GridLayout(3, 2));
		panel.add(new JLabel(" Number of Rows:"));
		panel.add(my_row_textfield);
		panel.add(new JLabel(" Number of Columns:  "));
		panel.add(my_column_textfield);
		
		JButton ok_button = new JButton("OK");
		ok_button.addActionListener(new OKActionListener());
		
		JButton cancel_button = new JButton("Cancel");
		cancel_button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
		    	my_edit_dialog.dispose();
			}
		});
		panel.add(ok_button);
		panel.add(cancel_button);
		return panel;
	}

	/**
	 * OKActionListener, class that listens to the OK Button action.
	 * 
	 * @author Hai Phan
	 */
	private class OKActionListener implements ActionListener {

		/**
		 * Invoked when the OK button is pressed.
		 * 
		 * @param the_event the event that was fired.
		 */
		@Override
		public void actionPerformed(ActionEvent the_event) {
			int rows = verifyRows();
			int columns = 0;
			if (rows > -1) {
				columns = verifyColumns();
			}
			if (rows > -1 && columns > -1) {
		    	if (my_table != null) {
			    	my_main_frame.remove(my_table);
			    }
		    	my_table = new SpreadSheetTable(rows, columns, my_formula_textfield);
		    	my_table.setVisible(true);
		    	my_gui.setTable(my_table);
		    	my_main_frame.add(BorderLayout.CENTER, my_table);
		    	my_main_frame.setVisible(true);
		    	my_formula_textfield.setText("");
		    	my_edit_dialog.dispose();
			}
		}
		
		/**
		 * Verify the valid user input for rows.
		 * 
		 * @return an integer value if the user entered a valid input; 
		 * 			-1, otherwise.
		 */
		private int verifyRows() {
			int rows = 0;
			String user_rows = my_row_textfield.getText();
	    	if (user_rows == null || user_rows.equalsIgnoreCase("")) {
	    		JOptionPane.showMessageDialog(null, "You did not enter a number for rows!", 
	    				"ERROR", JOptionPane.INFORMATION_MESSAGE);
	    		return -1;
	    	}
	    	try {
	    		rows = Integer.parseInt(user_rows);
	    		if (rows < 1) {
	    			throw new NumberFormatException();
		    	} 
	    	} catch (NumberFormatException e) {
	    		JOptionPane.showMessageDialog(null, "Bad Input @ Rows!", 
	    				"ERROR", JOptionPane.INFORMATION_MESSAGE);
	    		return -1;
	    	}
	    	return rows;
		}
		
		/**
		 * Verify the valid user input for columns.
		 * 
		 * @return an integer value if the user entered a valid input; 
		 * 			-1, otherwise.
		 */
		private int verifyColumns() {
			int columns = 0;
			String user_columns = my_column_textfield.getText();
	    	if (user_columns == null || user_columns.equalsIgnoreCase("")) {
	    		JOptionPane.showMessageDialog(null, "You did not enter a number for columns!", 
	    				"ERROR", JOptionPane.INFORMATION_MESSAGE);
	    		return -1;
	    	}
	    	try {
	    		columns = Integer.parseInt(user_columns);
	    		if (columns < 1) {
	    			throw new NumberFormatException();
		    	} 
	    	} catch (NumberFormatException e) {
	    		JOptionPane.showMessageDialog(null, "Bad Input @ Columns!", 
	    				"ERROR", JOptionPane.INFORMATION_MESSAGE);
	    		return -1;
	    	}
	    	return columns;
		}
	}

}
